package floappcontest.util.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import floappcontest.core.beans.Component;
import floappcontest.core.beans.Contest;
import floappcontest.core.beans.Faq;
import floappcontest.core.beans.FloappFile;
import floappcontest.core.beans.Student;
import floappcontest.core.beans.TeachingInstitution;
import floappcontest.core.beans.Team;
import floappcontest.core.beans.Tutor;
import floappcontest.core.beans.User;

public class HibernateUtil {
	
	private static final SessionFactory sessionFactory;
	private static final AnnotationConfiguration annotationConfiguration;
	
	static{
		try{
			annotationConfiguration = new AnnotationConfiguration();
			annotationConfiguration.addAnnotatedClass(Component.class);
			annotationConfiguration.addAnnotatedClass(Contest.class);
			annotationConfiguration.addAnnotatedClass(TeachingInstitution.class);
			annotationConfiguration.addAnnotatedClass(User.class);
			annotationConfiguration.addAnnotatedClass(Student.class);
			annotationConfiguration.addAnnotatedClass(Team.class);
			annotationConfiguration.addAnnotatedClass(Tutor.class);
			annotationConfiguration.addAnnotatedClass(Faq.class);
			annotationConfiguration.addAnnotatedClass(FloappFile.class);
			sessionFactory = annotationConfiguration.buildSessionFactory();
		} catch( Throwable ex ){
			ex.printStackTrace();
			throw new ExceptionInInitializerError( ex );
		}
	}
	
	public static Session getSession(){
		return sessionFactory.openSession();
		
	}
	
	public static void createDatabase(){
		new SchemaExport( annotationConfiguration ).create( false, true );
	}

	public static void dropDatabase(){
		new SchemaExport( annotationConfiguration ).drop( false, true );
	}

}
